#include "controlContactDialog.h"
controlContactDialog::controlContactDialog():QDialog()
{

	controlContactSta = controlContactDialog::cancel;
	this->setWindowTitle(cntr("操作联系人"));
	QVBoxLayout *mainVBoxLayout = new QVBoxLayout(this);

	/*姓名*/
	QHBoxLayout *nameHBoxLayout = new QHBoxLayout();
	mainVBoxLayout->addLayout(nameHBoxLayout);
	QLabel *labName = new QLabel(cntr("姓名"), this);
	nameHBoxLayout->addWidget(labName);
	editName = new QLineEdit(this);
	nameHBoxLayout->addWidget(editName);

	/*号码*/
	QHBoxLayout *phoneNumHBoxLayout = new QHBoxLayout();
	mainVBoxLayout->addLayout(phoneNumHBoxLayout);
	QLabel *labPhone = new QLabel(cntr("号码"), this);
	editPhone = new QLineEdit(this);
	phoneNumHBoxLayout->addWidget(labPhone);
	phoneNumHBoxLayout->addWidget(editPhone);
	QRegExpValidator *validator = new QRegExpValidator(QRegExp("[0-9]{0,11}"), this);
	editPhone->setValidator(validator);
	
	/*黑名单*/
	blackCheckBox = new QCheckBox(this);
	blackCheckBox->setText(cntr("黑名单"));
	mainVBoxLayout->addWidget(blackCheckBox);

	/*确定按钮*/
	QHBoxLayout *buttonHBoxLayout = new QHBoxLayout();
	mainVBoxLayout->addLayout(buttonHBoxLayout);

	saveButton = new QPushButton(this);
	saveButton->setText(cntr("修改"));
	buttonHBoxLayout->addWidget(saveButton);

	QPushButton *deleteButton = new QPushButton(this);
	deleteButton->setText(cntr("删除"));
	buttonHBoxLayout->addWidget(deleteButton);


	QPushButton *sendButton = new QPushButton(this);
	sendButton->setText(cntr("发短信"));
	buttonHBoxLayout->addWidget(sendButton);

	QPushButton *cancelButton = new QPushButton(this);
	cancelButton->setText(cntr("取消"));
	buttonHBoxLayout->addWidget(cancelButton);

	connect(saveButton, SIGNAL(clicked()), this, SLOT(changeStateSave()));
	connect(deleteButton, SIGNAL(clicked()), this, SLOT(changeStateDel()));
	connect(sendButton, SIGNAL(clicked()), this, SLOT(changeStateSend()));
	connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));

	/* 当编辑框发生改变时使能“保存”按钮 */
	connect(editName, SIGNAL(textEdited(const QString &)), this, SLOT(setSaveButtonEnable(const QString &)));
	connect(editPhone, SIGNAL(textEdited(const QString &)), this, SLOT(setSaveButtonEnable(const QString &)));
	connect(blackCheckBox, SIGNAL(stateChanged(int)), this, SLOT(blackStateChanged(int)));
}

void controlContactDialog::setContext(QString name, QString phoneNum, bool black)
{
	editName->setText(name);
	editPhone->setText(phoneNum);
	if(black == false)
	{
		blackCheckBox->setCheckState(Qt::Unchecked);
	}else{
		blackCheckBox->setCheckState(Qt::Checked);
	}
	//首先它处于不可点击状态，只有发生改变时才会可编辑
	saveButton->setEnabled(false);
}

void controlContactDialog::setSaveButtonEnable(const QString &str)
{
	saveButton->setEnabled(true);
}

void controlContactDialog::blackStateChanged(int state)
{
	saveButton->setEnabled(true);
}

